﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Taxonomy;
using System.Globalization;

namespace Ko2.Portal.SP_List
{
    public class SPList_ShopsDiscussions
    {

        #region Members 

        public string SiteUrlStr
        {
            get
            {
                return @"http://localhost/shops/";
            }
        }

        public string ListName
        {
            get
            {
                return @"Отзывы покупателей";
            }
        }

        #endregion

        public Exception Select(ref DataTable tbl)
        {
            Exception ret = null;
            DataTable tblTmp = new DataTable("tbl");
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = new SPSite(SiteUrlStr))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists[ListName];
                        SPQuery q = new SPQuery();
                        q.Query =
@"<Query>
   <OrderBy>
      <FieldRef Name='Title' />
   </OrderBy>
</Query>";
                        SPListItemCollection items = list.GetItems(q);
                        tblTmp = items.GetDataTable();
                    }
                }
            });
            if (tblTmp != null)
                tbl = tblTmp;
            else
                tbl = new DataTable("tbl");
            return ret;
        }

        public Exception Insert(DateTime parDT, string parTitle, string parShopName, string parBody)
        {
            Exception ret = null;
            //Term term = null;
            int lcid = CultureInfo.CurrentCulture.LCID;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SiteUrlStr))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists[ListName];
                            SPListItem item = list.Items.Add();

                            //item["Title"] = parTitle;
                            if (!String.IsNullOrEmpty(parShopName))
                                item["Title"] = parShopName;
                            item["Основной текст"] = parBody;

                            //term = null;
                            //try
                            //{
                            //    item["Title"] = parShopName;
                            //    term = MetadataHelper.FindItemByName(SiteUrlStr, @"Магазины", parShopName, lcid);
                            //}
                            //catch { }
                            //if (term != null)
                            //{
                            //    MetadataHelper.SetFieldValueTerm(item, "Магазин", term);
                            //}
                                                        
                            item.Update();

                        }
                    }
                });
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }

    }
}
